window.addEventListener("load", ds_addonDonateInit, true);
var enzy_pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
var enzy_observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);

function ds_addonDonateInit() {

	// Test pref to see if we should add/supress donate text
	try {
		var supDonateText = enzy_pref.getBoolPref("downsort.function.supressDonateText");
		if(supDonateText)
			return;
	} catch (e) {}
	
	window.addEventListener('unload', ds_addonDonateClose, false);
	enzy_observerService.addObserver(ds_observer,"removeDonatePressed",false);
	
	ds_gExtensionsView = document.getElementById("extensionsView");
	ds_gExtensionsView.addEventListener("select", ds_setDonateText, false);
	ds_setDonateText();
	
	// Add this to make sure text is there after switching groups (buttons at top)
	var viewGroup = document.getElementById("viewGroup");
	var currCommand = viewGroup.getAttribute("onclick");
	viewGroup.setAttribute("onclick", currCommand + " window.setTimeout(ds_setDonateText, 200);");
}

function ds_setDonateText() {
	
	// Check if the text is already there, if so return
	if(document.getElementById("ds_donateContainerOn"))
		return;
	
	var myext = document.getElementById("urn:mozilla:item:{D9808C4D-1CF5-4f67-8DB2-12CF78BBA23F}");
	
	var donateContainer = document.getElementById("donateContainer");
	//var donateText1 = document.getElementById("donateTextNode1");
	//var donateText2 = document.getElementById("donateTextNode2");
	var donateSpacer = document.getElementById("donateSpacer");
	
	var nameVersionBox;
	try {
		// this ext is currently selected
		nameVersionBox = document.getAnonymousElementByAttribute(myext, "anonid", "addonNameVersion");
		if(!nameVersionBox)  // this ext is not currently selected
			nameVersionBox = document.getAnonymousElementByAttribute(myext, "class", "addon-name-version");
		
		var spacerClone = donateSpacer.cloneNode(true);
		spacerClone.hidden = false;
		//spacerClone.id = "donateSpacerOn";
		nameVersionBox.appendChild(spacerClone);
		var containerClone = donateContainer.cloneNode(true);
		containerClone.id = "ds_donateContainerOn";
		containerClone.hidden = false;
		nameVersionBox.appendChild(containerClone);
		//nameVersionBox.appendChild(donateText1.cloneNode(true));
		//nameVersionBox.appendChild(donateText2.cloneNode(true));
		//donateNode.hidden = false;
	} catch (e) {}
	
}

function ds_openDonate() {
	
	// Open page in new tab
	var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService();
    var wmed = wm.QueryInterface(Components.interfaces.nsIWindowMediator);
    
    var win = wmed.getMostRecentWindow("navigator:browser");
    if (!win)
    	win = window.openDialog("chrome://browser/content/browser.xul", "_blank", "chrome,all,dialog=no", "http://downloadstatusbar.mozdev.org/aboutDonations.html", null, null);
    else {
    	var content = win.document.getElementById("content");
    	content.selectedTab = content.addTab("http://downloadstatusbar.mozdev.org/aboutDonations.html");	
    }
    
	// Set pref not to show donate text?
}

// Using observer service to do this so that if more than one of my extensions is installed, all of them can respond to the same keypress
function enzy_removeDonate() {
	
	enzy_observerService.notifyObservers(null,"removeDonatePressed", null);
}

var ds_observer = {
observe: function(subject,topic,data) {
	
	enzy_pref.setBoolPref("downsort.function.supressDonateText", true);
	//var ds_strings = document.getElementById("downsortbundle");
	alert("Download Sort\nDonate message will be removed next time the add-ons window is reopened.\n\nIf you donated, thanks!");
	
  }
};

function ds_addonDonateClose() {
	
	enzy_observerService.removeObserver(ds_observer,"removeDonatePressed");
	
}
